Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to add min and max values to decimals in preferences page #232

Merged
merged 3 commits into from
Jun 8, 2024

Conversation

JeremyStorring
Copy link
Contributor

Closes #189

I've made it so that remaining carbs cap will not go less than 90 based on the docs. I've also added the functionality to set constraints to values (any safety setting we decide on in the future).

dnzxy
dnzxy previously approved these changes May 26, 2024
Copy link
Contributor

@dnzxy dnzxy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Copy link
Contributor

@AndreasStokholm AndreasStokholm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

bjornoleh
bjornoleh previously approved these changes May 26, 2024
Copy link
Contributor

@bjornoleh bjornoleh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Besides the remainingCarbsCap min value of 90 g, LGTM.

When testing entering a value below the 90 g min limit for remainingCarbsCap, the value is immediately changed to 90 g when tapping Done. There is no feedback otherwise, but that may not be needed. The important part is being able to disallow invalid settings, while still showing what setting is being used.

@MikePlante1
Copy link
Contributor

Oh yeah, I also meant to test or ask about what happens when someone already set something outside of the guardrails before the guardrail was added. Will it just silently reset it within the guardrails the next time the preference menu is open? We should also at least an an issue ticket to the docs repo to add instructions to alter guardrails in the customization section of the docs

Maybe a popup/alert should happen whenever it’s reset? It could declare the enforced limits

bjornoleh
bjornoleh previously approved these changes May 26, 2024
@MikePlante1
Copy link
Contributor

MikePlante1 commented May 26, 2024

And not sure if some actual guardrails should be included in this PR or wait for a future PR, but here are some suggestions:

Max Daily Safety Multiplier: 1 -
Current Basal Safety Multiplier: 1 -
Autosens Max: 1 - 3
Autosens Min: 0.1 - 1
AF: 0.1 - 3
Sigmoid AF: 0.1 - 2
Weight of past 24hr vs 10days: 0 - 1
Threshold: 60 - 120
Max Delta-BG Threshold: 0 - 0.4
Enable SMB When Glucose Is Over: 40 - 399
Max SMB Basal Mins: 0 - 180
Max UAM SMB Basal Mins: 0 - 180
SMB Delivery Ratio: 0 - 1
Bolus Increment: 0.025 -
Autotune ISF AF: 0 - 1

@AndreasStokholm
Copy link
Contributor

AndreasStokholm commented May 26, 2024

Oh yeah, I also meant to test or ask about what happens when someone already set something outside of the guardrails before the guardrail was added. Will it just silently reset it within the guardrails the next time the preference menu is open? We should also at least an an issue ticket to the docs repo to add instructions to alter guardrails in the customization section of the docs

Maybe a popup/alert should happen whenever it’s reset? It could declare the enforced limits

I think it's is incredibly important to define. IMO throwing a pop-up telling people that this is changed (on save), and then we should have a discussion on what should happen after that. I can see the following ways of handling it:

  1. Clamp to whatever end of the guardrail that is closer
  2. Require the user to set it themselves
  3. Start warning on settings save in one version, but not clamp or require any user input changes. Then in a later version we start doing either 1 or 2.

I am not personally sure which approach is better.

@bjornoleh
Copy link
Contributor

The 90 g min limit did clamp values below to 90. There was no max value, so I don’t know if the same happens when above (clamping to the closest limit?)

@bjornoleh
Copy link
Contributor

bjornoleh commented May 26, 2024

I can confirm that the current implementation clamps the setting to the closest limit, after testing to introduction of an arbitrary max limit as well:

                Field(
                    displayName: NSLocalizedString("Remaining Carbs Cap", comment: "Remaining Carbs Cap"),
                    type: .decimal(keypath: \.remainingCarbsCap, minVal: 90, maxVal: 100),
                    infoText: NSLocalizedString(
                        "This is the amount of the maximum number of carbs we’ll assume will absorb over 4h if we don’t yet see carb absorption.",
                        comment: "Remaining Carbs Cap"
                    ),

< min limit = min limit
> max limit = max limit

(settings in between are accepted as is)

@mountrcg
Copy link
Contributor

Just tested this on some max/min for decimal prefs. Works as suggested and if going beyond min/max it will restricted to applicable min/max. On Bool prefs it throws an error while configuring it. So all good!!v

@mountrcg
Copy link
Contributor

I suggest to define the min/max limits in FreeAPS/Sources/Models/Preferences.swift though, instead of in the FreeAPS/Sources/Modules/PreferencesEditor/PreferencesEditorStateModel.swift

@bjornoleh
Copy link
Contributor

I suggest to define the min/max limits in FreeAPS/Sources/Models/Preferences.swift though, instead of in the FreeAPS/Sources/Modules/PreferencesEditor/PreferencesEditorStateModel.swift

That might be a good idea for maintainability, then defaults and min/max limits could be stored the same place?

So here:
https://github.com/nightscout/Trio/blob/dev/FreeAPS/Sources/Models/Preferences.swift

Instead of here:

type: .decimal(keypath: \.remainingCarbsCap, minVal: 90),

@JeremyStorring , thoughts?

@JeremyStorring
Copy link
Contributor Author

I suggest to define the min/max limits in FreeAPS/Sources/Models/Preferences.swift though, instead of in the FreeAPS/Sources/Modules/PreferencesEditor/PreferencesEditorStateModel.swift

That might be a good idea for maintainability, then defaults and min/max limits could be stored the same place?

So here: https://github.com/nightscout/Trio/blob/dev/FreeAPS/Sources/Models/Preferences.swift

Instead of here:

type: .decimal(keypath: \.remainingCarbsCap, minVal: 90),

@JeremyStorring , thoughts?

I can definitely give this a shot, but it might be a bit harder to implement :)

@avouspierre
Copy link
Contributor

@JeremyStorring

I tested the PR with success.

I suggest to define the min/max limits in FreeAPS/Sources/Models/Preferences.swift though, instead of in the FreeAPS/Sources/Modules/PreferencesEditor/PreferencesEditorStateModel.swift
I can definitely give this a shot, but it might be a bit harder to implement :)

You could use the same approach as the key path with this suggestion (I quickly tested) :

case decimal(
            keypath: WritableKeyPath<Preferences, Decimal>,
            minVal: WritableKeyPath<Preferences, Decimal>? = nil,
            maxVal: WritableKeyPath<Preferences, Decimal>? = nil
        )
....
if let minValue = minVal, let minValueDecimal: Decimal = settable?.get(minValue),
                   let maxValue = maxVal, let maxValueDecimal: Decimal = settable?.get(maxValue)
                {
                    constrainedValue = min(max(value, minValueDecimal), maxValueDecimal)
                } else if let minValue = minVal, let minValueDecimal: Decimal = settable?.get(minValue) {
                    constrainedValue = max(value, minValueDecimal)
                } else if let maxValue = maxVal, let maxValueDecimal: Decimal = settable?.get(maxValue) {
                    constrainedValue = min(value, maxValueDecimal)
                } else {
                    constrainedValue = value
                }

and after just to write each min / max with :

Field(
                    displayName: NSLocalizedString("Remaining Carbs Cap", comment: "Remaining Carbs Cap"),
                    type: .decimal(
                        keypath: \.remainingCarbsCap,
                        minVal: \.remainingCarbsCap_min,
                        maxVal: \.remainingCarbsCap_max
                    ),
                    ....
                ),

and specify in preferences.swift :

 var remainingCarbsCap_min: Decimal = 80
    var remainingCarbsCap_max: Decimal = 100

@JeremyStorring JeremyStorring changed the base branch from dev to alpha June 1, 2024 23:05
@JeremyStorring
Copy link
Contributor Author

JeremyStorring commented Jun 1, 2024

minVal: WritableKeyPath<Preferences, Decimal>? = nil,
maxVal: WritableKeyPath<Preferences, Decimal>? = nil

Oh boy I completely misunderstood what was being asked, yeah this makes sense and I think would be a better implementation!

I didn't actually add min and max values, but it works as intended once we updated Preferences.

Also disregard git fail from above :P

@bjornoleh
Copy link
Contributor

Thanks!

Could you please add min/max limits for one or more of the settings that are likely to be agreement on, as an example to ease the code review and testing?

Such as:

Weight of past 24hr vs 10days: 0 - 1
Threshold: 60 - 120
SMB Delivery Ratio: 0 - 1
Autotune ISF AF: 0 - 1

Copy link
Contributor

@MikePlante1 MikePlante1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested it with the threshold setting for 60-120 and it worked as expected.

I think a popup alert notifying the user that their entry has been nudged would be nice, but that could be a new PR.

@dnzxy
Copy link
Contributor

dnzxy commented Jun 7, 2024

As. this only adds functionality to limit preferences and does not actually introduce any limits (for now) LGTM.

@Sjoerd-Bo3
Copy link
Contributor

Merging with 2 approvals and mine😇

@Sjoerd-Bo3 Sjoerd-Bo3 merged commit 5509c9b into nightscout:alpha Jun 8, 2024
1 check passed
Sjoerd-Bo3 added a commit that referenced this pull request Jun 8, 2024
PreferencesEditorDataFlow: commit Xcode auto formatting related to #232
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Remaining carbs cap allows 'illegal' value
8 participants